﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telerik.OpenAccess.Metadata;

namespace LINQPad.OpenAccess.Metadata
{
    /// <summary>
    /// <see cref="IContextTypeInfo"/> equality comparer implementation.
    /// </summary>
    public partial class ContextTypeInfoEqualityComparer : EqualityComparer<IContextTypeInfo>
    {
        #region EqualityComparer<IContextTypeInfo> Members

        /// <summary>
        /// Returns whether the two instances are logically equal.
        /// </summary>
        /// <param name="x">The x.</param>
        /// <param name="y">The y.</param>
        /// <returns>
        ///   <c>true</c> if they are logically equal; otherwise, <c>false</c>.
        /// </returns>
        public override bool Equals( IContextTypeInfo x, IContextTypeInfo y )
        {
            if ( x == null )
                return y == null;
            else if ( y == null )
                return false;
            else
                return EqualityComparer<Type>.Default.Equals( x.ContextType, y.ContextType )
                        && EqualityComparer<string>.Default.Equals( x.Name, y.Name )
                        && EqualityComparer<MappingDefinitionType?>.Default.Equals( x.MappingType, y.MappingType );
        }
        /// <summary>
        /// Returns a hash code for this instance.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <returns>
        /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. 
        /// </returns>
        public override int GetHashCode( IContextTypeInfo obj )
        {
            int hash = 0;

            if ( obj != null )
            {
                hash ^= 3 * EqualityComparer<Type>.Default.GetHashCode( obj.ContextType );
                hash ^= 7 * EqualityComparer<string>.Default.GetHashCode( obj.Name );
                hash ^= 11 * EqualityComparer<MappingDefinitionType?>.Default.GetHashCode( obj.MappingType );
            }

            return hash;
        }

        #endregion
    }
}
